package foundation.concurrency;

public abstract class Consummer<T> implements Runnable {
	Product<T> prod = null;
	Thread thread = null;
	//判断算法一次运行完后，插入数据库是否完成
	boolean isFinish = false;
	public Consummer(Product<T> prod) {
		this.prod = prod;
		thread = new Thread(this);
	}

	protected  abstract void consumeProduct(T data); 
	
	@Override
	public void run() {
		int maxNum = prod.getMaxNum();
		for (int i = 0; i < maxNum; i++) {
			T data = prod.getData();
			consumeProduct(data);
		}
		//算法一次运行完后，插入数据库完成
		isFinish = true;
	}

	public synchronized void start() {
		thread.start();
	}

	public synchronized void join(long milli) throws InterruptedException {
		thread.join(milli);
	}

	public boolean isFinish() {
		return isFinish;
	}

	public void setFinish(boolean isFinish) {
		this.isFinish = isFinish;
	}
}
